package com.erp.module.crm.xXCrm.guiMingXi

import android.app.Activity
import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activity.BaseActivity
import com.erp.activity.ChaKanTuZhiAvtivity
import com.erp.activity.GonggaoTextActivity
import com.erp.bean.ProdyctionClientBean
import com.erp.module.crm.xXCrm.FragmentClientfive
import com.erp.module.xianChangPaiZhao.XianChangPaiZhapModel
import com.erp.myAdapter.GuimingxiAdapter
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.view.MyAlerDialog
import kotlinx.android.synthetic.main.activity_guimingxi.*
import kotlinx.android.synthetic.main.dialog_etyuliaocangwei.CangweiEt
import kotlinx.android.synthetic.main.dialog_etyuliaocangwei.LeftBt
import kotlinx.android.synthetic.main.dialog_etyuliaocangwei.RightBt
import kotlinx.android.synthetic.main.dialog_etyuliaocangwei.title
import kotlinx.android.synthetic.main.fragment_clientfive.llBaomingxi
import kotlinx.android.synthetic.main.fragment_clientfive.llGuimingxi
import kotlinx.android.synthetic.main.fragment_clientfive.llWuliao
import kotlinx.android.synthetic.main.header_knoya.*
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Administrator on 2016/12/28/028.
 * 作者：刘洋
 */
class GuimingxiActivity : BaseActivity(),GuimingxiInterface {
    lateinit var listguimingxi: ArrayList<ProdyctionClientBean.guimingxi>
    lateinit var guimingxiAdapter: GuimingxiAdapter
    lateinit var updatedialog: AlertDialog
    private val a = "GuimingxiActivity"
    var ischa = false
    var onum: String = ""
    var oid : String = ""
    var model = XianChangPaiZhapModel()
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_guimingxi
    }

    override fun initCreate() {
        ischa = intent.getBooleanExtra("chaxun",false)
        iv_header_left.visibility = View.VISIBLE
        onum = intent.getStringExtra("shengchan")
        oid = intent.getStringExtra("oid")
        listguimingxi = ArrayList<ProdyctionClientBean.guimingxi>()
        if(ischa){
            getData()
        }else{
            listguimingxi = FragmentClientfive.getguimingxi()
        }
        guimingxiAdapter = GuimingxiAdapter(listguimingxi, this,onum,this)
        LvGuimingxi.adapter = guimingxiAdapter
        tv_header_title.text = onum
        iv_header_left.setOnClickListener { finish() }
    }
    fun getData(){
        updatedialog = Myutil.showupdatedialog(this@GuimingxiActivity, "正在获取图纸数据", false)
        model.getAnZhuangXingxi(this@GuimingxiActivity, oid, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                jsondata(data)
                guimingxiAdapter.notifyall(listguimingxi)
                updatedialog.dismiss()
            }

            override fun onNot(throwable: String) {
                Myutil.MyTost("获取数据失败")
                updatedialog.dismiss()
            }
        })
    }
    fun getGuiData(){
        updatedialog = Myutil.showupdatedialog(this@GuimingxiActivity, "正在刷新数据", false)
        Httputil.getHttpProdyctionClient(this, oid, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                updatedialog.dismiss()
                listguimingxi = JsonUtil.JsonProdyction(data).guimingx
                guimingxiAdapter.notifyall(listguimingxi)
            }

            override fun onNot(throwable: String) {
                updatedialog.dismiss()
            }
        })
    }
    fun jsondata(data : String){
        try{
            val jo = JSONObject(data)
            val joextension = JsonUtil.getjovolue(jo, "extension")
            var joo = JsonUtil.getjovolue(joextension, "order")
            if (JsonUtil.getisjo(joo ,"cabinet")) {
                val js1 = joo.getJSONArray("cabinet") as JSONArray
                for (i in 0 until js1.length()) {
                    val jo1 = js1.get(i) as JSONObject
                    val guimingxi = ProdyctionClientBean.guimingxi()
                    guimingxi.guihao = JsonUtil.getvolue(jo1, "cnum")
                    guimingxi.chaidanyuan = JsonUtil.getvolue(jo1, "dismantler_name")
                    guimingxi.chaidanshijian = JsonUtil.getvolue(jo1, "c_dismantle_time")
                    guimingxi.rucangshijian = JsonUtil.getvolue(jo1, "c_warehousing_time")
                    guimingxi.fahuoshijian = JsonUtil.getvolue(jo1, "c_deliver_time")
                    guimingxi.zhuangtai = JsonUtil.getvolue(jo1, "status_name")
                    guimingxi.cte_time = JsonUtil.getvolue(jo1, "cte_time")
                    guimingxi.cte_problem = JsonUtil.getvolue(jo1, "cte_problem")
                    val jsguitu = JsonUtil.getjsvolue(jo1, "tz_png_array")
                    val tuzhis = ArrayList<String>()
                    for (t in 0 until jsguitu.length()) {
                        tuzhis.add(jsguitu.get(t).toString())
                    }
                    guimingxi.tuzhis = tuzhis
                    guimingxi.id = JsonUtil.getvolue(jo1, "id")
                    guimingxi.name = JsonUtil.getvolue(jo1, "name")
                    listguimingxi.add(guimingxi)
                }
            }
        }catch (e : JSONException){
            e.printStackTrace()
        }
    }

    override fun clickCte(finalGuimingxi: ProdyctionClientBean.guimingxi) {
        if(finalGuimingxi.cte_time == ""){
            val dialog = AlertDialog.Builder(this).create()
            dialog.setView(EditText(this))
            dialog.show()
            dialog.setContentView(R.layout.dialog_etyuliaocangwei)
            dialog.CangweiEt.setLines(20)
            val window = dialog.window
            dialog.title.text = "确认试装"
            dialog.CangweiEt.hint = "填写试装问题"
            val p = window?.attributes // 获取对话框当前的参数值
            if (p != null) {
                p.width = (sp.getInt("width", 0) * 0.9).toInt()
            } // 宽度设置为屏幕的0.65
            if (window != null) {
                window.attributes = p
            }
            dialog.LeftBt.setOnClickListener {
                dialog.dismiss()
                val problem = dialog.CangweiEt.text.toString();
                updatedialog = Myutil.showupdatedialog(this@GuimingxiActivity, "正在确认试装", false)
                model.cabinetTrialErection(this,finalGuimingxi.id,problem,object : OnUserDataInterface{
                    override fun onUserData(data: String?) {
                        updatedialog.dismiss()
                        Myutil.MyTost("柜试装成功")
                        getGuiData()
                    }

                    override fun onNot(throwable: String?) {
                        updatedialog.dismiss()
                    }

                })
            }
            dialog.RightBt.setOnClickListener {
                dialog.dismiss()
            }
        }else{
            val dialog = AlertDialog.Builder(this)
            dialog.setTitle("提示")
            dialog.setMessage("确定要取消柜试装吗？")
            dialog.setPositiveButton("确定") { dialog, _ ->
                dialog.dismiss()
                updatedialog = Myutil.showupdatedialog(this@GuimingxiActivity, "正在取消试装", false)
                model.deleteCabinetTrialErection(this,finalGuimingxi.id,object : OnUserDataInterface{
                    override fun onUserData(data: String?) {
                        updatedialog.dismiss()
                        Myutil.MyTost("柜试装成功")
                        getGuiData()
                    }

                    override fun onNot(throwable: String?) {
                        updatedialog.dismiss()
                    }

                })
            }
            dialog.setNegativeButton("取消") { dialog, which -> dialog.dismiss() }
            dialog.show()
        }
    }

    override fun clickTuzhi(finalGuimingxi : ProdyctionClientBean.guimingxi) {
        val guituzis: ArrayList<String> = finalGuimingxi.tuzhis
        if (guituzis.size > 0) {
            val intent = Intent(this, ChaKanTuZhiAvtivity::class.java)
            intent.putExtra("onum", onum)
            intent.putExtra("guihao", finalGuimingxi.guihao)
            intent.putStringArrayListExtra("url", guituzis)
            startActivity(intent)
        }else{

            val intent = Intent(this, GonggaoTextActivity::class.java)
            val qrCode = onum+"-"+finalGuimingxi.guihao
            var url = "https://qdsoft.huaguangsoft.com/htmloutput/App/Treasure/index.html?company=401_qdsoft&qrCode=$qrCode#/";
            intent.putExtra("url", url)
            intent.putExtra("title","查看图纸")
            startActivity(intent)
        }
    }
}
